﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

namespace Dev01
{
    class Utilities
    {
        public string KeTiep(DataTable dtable, string name, string prefix)
        {
            DataTable dt = dtable;
            int count, id;
            string next = "";
            count = dt.Rows.Count;
            if (count != 0)
            {
                int fist = DoiMaSo(dt.Rows[0][0].ToString(), prefix);
                next = LayMaSo(dt.Rows[count - 1][0].ToString(), prefix);
                if (fist != 1)
                    next = LayMaSo(name, prefix);
                else
                {
                    id = 1;
                    for (int i = 0; i < count; i++)
                    {

                        if (id != DoiMaSo(dt.Rows[i][0].ToString(), prefix))
                        {
                            next = LayMaSo(dt.Rows[i - 1][0].ToString(), prefix);
                            break;
                        }
                        id++;
                    }
                }
            }
            else
                next = LayMaSo(name, prefix);
            return next;
        }
        public string LaySTTKeTiep(DataTable dt)
        {
            int fisrt = 0, next = 0, last = 0;
            int count = dt.Rows.Count;
            string id = "";
            if (count != 0)
            {
                fisrt = int.Parse(dt.Rows[0][0].ToString());
                last = int.Parse(dt.Rows[count - 1][0].ToString());
                if (fisrt != 1)
                    id = "1";
                else
                {
                    for (int i = 0; i < count; i++)
                    {
                        next = int.Parse(dt.Rows[i][0].ToString());
                        if (fisrt != next)
                        {
                            id = fisrt.ToString();

                            break;
                        }
                        else if (last == next)
                        {
                            id = (last + 1).ToString();
                            break;
                        }
                        else
                            fisrt++;
                    }
                }
            }
            else id = "1";
            return id;
        }
        public string LayMaSo(string name, string prefix)
        {
            int nextID = int.Parse(name.Remove(0, prefix.Length)) + 1;
            int lengthNumberID = name.Length - prefix.Length;
            string zeroNumber = "";
            for (int i = 1; i <= lengthNumberID; i++)
            {
                if (nextID < Math.Pow(10, i))
                {
                    for (int j = 1; j <= lengthNumberID - i; j++)
                    {
                        zeroNumber += "0";
                    }
                    return prefix + zeroNumber + nextID.ToString();
                }
            }
            return prefix + nextID;
        }
        public int DoiMaSo(string name, string prefix)
        {
            int Ma = int.Parse(name.Remove(0, prefix.Length));
            return Ma;
        }
    }
}
